package com.yuelan.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuelan.apartment.domain.ApaRentBill;
import com.yuelan.apartment.mapper.ApaRentBillMapper;
import com.yuelan.app.domain.RentPayOrder;
import com.yuelan.app.mapper.OrderMapper;
import com.yuelan.app.service.OrderService;
import com.yuelan.common.utils.DateUtils;
import com.yuelan.common.utils.uuid.IdWorker;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import static com.yuelan.app.constant.BillType.Collected;

/**
 * @description:
 * @Author: ZhaoYi
 * @date: 24-7-11 21:52
 */

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private ApaRentBillMapper apaRentBillMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Async(value = "taskRentOrderExecutor")
    public void createRentPayOrder(RentPayOrder rentPayOrder) {
        RentPayOrder orderInfo = rentPayOrderInfo(rentPayOrder);
        orderMapper.insert(orderInfo);
        // 更新账单状态
        LambdaUpdateWrapper<ApaRentBill> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApaRentBill::getId, rentPayOrder.getId());
        updateWrapper.set(ApaRentBill::getStatus, Collected.getStatus());
        apaRentBillMapper.update(updateWrapper);
    }

    private RentPayOrder rentPayOrderInfo(RentPayOrder rentPayOrder) {
        RentPayOrder payOrder = new RentPayOrder();
        payOrder.setId(IdWorker.snowflake());
        payOrder.setUserId(rentPayOrder.getUserId());
        payOrder.setPaymentMethod("微信支付");
        payOrder.setPaymentStatus("success");
        payOrder.setPaymentAmount(rentPayOrder.getPaymentAmount());
        payOrder.setPaymentDate(DateUtils.getNowDate());
        payOrder.setCreatedAt(DateUtils.getNowDate());
        payOrder.setUpdatedAt(DateUtils.getNowDate());
        return payOrder;
    }
}
